#Working MHK header document
#Simon B. Woodside <sbwoodside@undergrad.math.uwaterloo.ca> <http://simonwoodside.home.ml.org>
#July 20, 1998
#My comments are indicated by a hash (#) mark


From d88-bli@nada.kth.se Thu Mar 26 23:41:40 1998
Date: Thu, 26 Mar 1998 20:21:21 +0100 (MET)
From: Bo Lindbergh <d88-bli@nada.kth.se>
To: sbwoodsi@picard.math.uwaterloo.ca
Subject: Re: Riven sound file hacking

No idea about sounds, but here is the overall mhk file format as I deduced
it while making RMS.

/Bo Lindbergh


===== cut here ===== cut here ===== cut here ===== cut here =====
A mhk file has the following overall structure:
    File header
    Resource directory
    Individual resources
All integer fields use big-endian byte order.

#not all of the header sections are exactly as described, so we will
#need to use the offsets given to find them. Especially local and globals tables!
The file header looks like this:
    4 bytes, magic number                   always "MHWK"
    4 bytes, (file length)-8,
    4 bytes, file type                      always "RSRC"
    4 bytes, version?                       always 01000000 hex
    4 bytes, file length
    4 bytes, offset of directory            always 0000001C hex
    2 bytes, offset (within directory) of global resource table
    2 bytes, size of global resource table

The resource directory looks like this:
    resource type table
    local resource tables (one per type)
    resource names
    global resource table

The resource type table looks like this:
    2 bytes, offset (within directory) of resource names
    2 bytes, number of resource types
    for each resource type:
        4 bytes, resource tag               "tMOV" for movies, etc.
        2 bytes, offset (within directory) of start of local table
        2 bytes, offset (within directory) of end of local table

Each local resource table looks like this:
    2 bytes, number of resources of this type
    for each resource:
        2 bytes, local resource id
        2 bytes, global resource index (1-based!)
    2 bytes, number of resources (again)	#2 bytes, sometimes 0
    #actually:
    #record: 2 bytes, num of this type
    #	for each:	2 bytes, local resource id, 1-based (most of the time)
    #				2 bytes, global resource index, 1-based
    #after records there are 2 bytes, mostly 0.
    #NOTES:  SLST resource table repeats using the same amount
    # 	of space, but may contain different data the second time.
    #	(string list?  mac vs pc?)
    # ALSO, SLST doesn't terminate with a 2-byte 0.
    # ALSO, tMOV doesn't terminate with a 2-byte 0.
    # ALSO, tWAV doesn't terminate with a 2-byte 0.
    # The tWAV resource repeats using the same amount of space.
    # There may be extra crap at the end of the local resource
    #	table
    # SO, we actually have to use the offsets to read these guys reliably

The resource names are simple zero-terminated strings.
#ONLY resources with the character "t" as the first character
#of their resource type have names.
#sometimes the names are out of order!! (eg. a_data.mhk at the end)

The global resource table looks like this:
    4 bytes, number of resources
    for each resource:
        4 bytes, offset (within file) of resource
        2 bytes, bits 15-0 of resource size
        1 byte, bits 23-16 of resource size
        1 byte, resource flags
        2 bytes, unknown	#always 0
